package RESP

import "strconv"

type MapData struct {
	data map[string]string
}


func MakeMapData(m map[string]string) *MapData {
	return &MapData{data:m}
}
func MakeEmptyMapData() *MapData {
	return &MapData{data:map[string]string{}}
}
func (a *MapData) ToBytes() []byte {
	if a.data == nil {
		return []byte("%-1" + CRLF)
	}
	res := []byte("%" + strconv.Itoa(len(a.data)) + CRLF)
	for k, v := range a.data {
		s:="$"+strconv.Itoa(len(k))+CRLF+k+CRLF+"$"+strconv.Itoa(len(v))+CRLF+v+CRLF
		res = append(res, s...)
	}
	return res
}
func (a *MapData) Data() map[string]string {
	return a.data
}
func (a *MapData) ToCommand() [][]byte {
	res := make([][]byte, 0)
	for k, v := range a.data {
		s:="$"+strconv.Itoa(len(k))+CRLF+k+CRLF+"$"+strconv.Itoa(len(v))+CRLF+v+CRLF
		res = append(res, []byte(s))
	}
	return res
}
func (a *MapData) ByteData() []byte {
	res := make([]byte, 0)
	for k, v := range a.data {
		s:="$"+strconv.Itoa(len(k))+CRLF+k+CRLF+"$"+strconv.Itoa(len(v))+CRLF+v+CRLF
		res = append(res, s...)
	}
	return res
}
